<?php

namespace App\Admin\Controllers;

use App\Luodiye;
use App\model\WechatUser;
use App\WechatLink;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class LuodiyeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '落地页';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Luodiye());

        $grid->column('id', __('Id'));
        $grid->column('name', '落地页名称');
        $grid->column('url','落地页链接')->display(function ($value){
            return route('luodiye',$this->id);
        });
        $grid->column('link_id', '联系人链接')->display(function ($value){
         $link=   WechatLink::find($value);
         if ($link){
             return WechatUser::whereIn('userid',$link->user_ids)->pluck('name')->implode(',');
         }
         return  '';
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Luodiye::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('link_id', __('Link id'));
        $show->field('images', __('Images'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Luodiye());
        $form->text('name', '落地页名称')->required();
        $form->select('link_id', '选择联系人')->options(WechatLink::all()->pluck('name', 'id'));
        $form->multipleImage('images', '上传长图');
        return $form;
    }
}
